<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">{{ title }}</h3>
      </div>
      <el-form-item prop="userName">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          v-model.trim="loginForm.userName"
          placeholder="用户名"
          name="userName"
          type="text"
          tabindex="1"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
      </el-form-item>
      <el-form-item prop="password" style="margin-bottom: 10px">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model.trim="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>
      <div style="margin-bottom: 10px">
        <el-checkbox v-model="loginForm.checkLog" style="color: #FFF">是否自动登录</el-checkbox>
      </div>
      <el-button
        :loading="loading"
        type="primary"
        style="width:100%;margin-bottom: 30px;"
        @click.native.prevent="handleLogin"
      >
        {{ $t('market.login') }}<!--登录-->
      </el-button>
      <span style="color: white">
        推荐使用Chrome浏览器，可以获得更佳页面操作体验
      </span>
      <a
        href="https://www.google.cn/intl/zh-CN/chrome/"
        style="color: #409eff; text-decoration:underline"
        target="_blank"
      >
        https://www.google.cn/chrome/
      </a>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        userName: '',
        password: '',
        checkLog: false
      },
      demo: 'jc',
      title: '三幸运营平台',
      loginRules: {
        userName: [
          { required: true, trigger: 'blur', message: '账号不能为空' }
        ],
        password: [{ required: true, trigger: 'blur', message: '密码不能为空' }]
      },
      mlgArr: ['mlg', 'jnk', 'mr', 'ja'],
      xingArr: ['fa', 'mw', 'jc', 'ct', 'xgjx'],
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    if (location.pathname.indexOf('jc-web') !== -1) { // 惠州杰希
      this.demo = 'jc'
      this.title = '三幸运营平台(惠州杰希)'
    } else if (location.pathname.indexOf('mlg-web') !== -1) { // 摩洛哥
      this.demo = 'mlg'
      this.title = '三幸运营平台(摩洛哥)'
    } else if (location.pathname.indexOf('fa-web') !== -1) { // 惠州杰希外销
      this.demo = 'fa'
      this.title = '三幸运营平台(惠州杰希外销)'
    } else if (location.pathname.indexOf('jnk-web') !== -1) { // 摩洛哥-jnk
      this.demo = 'jnk'
      this.title = '三幸运营平台(摩洛哥-JMK)'
    } else if (location.pathname.indexOf('mw-web') !== -1) { // 麦斯威尔
      this.demo = 'mw'
      this.title = '三幸运营平台(MAXWILL TECHNOLOGY CO.,LIMITED)'
    } else if (location.pathname.indexOf('mr-web') !== -1) { // 麦斯威尔
      this.demo = 'mr'
      this.title = '三幸运营平台(广东麦斯威尔)'
    } else if (location.pathname.indexOf('kj-web') !== -1) { // 跨境电商
      this.demo = 'kj'
      this.title = '香港魔方盒子电子商务有限公司'
    } else if (location.pathname.indexOf('ja-web') !== -1) { // Atlas Sofa
      this.demo = 'ja'
      this.title = '三幸运营平台(Atlas Sofa)'
    } else if (location.pathname.indexOf('xgjx-web') !== -1) { // 香港杰希
      this.demo = 'xgjx'
      this.title = '三幸运营平台(香港杰希)'
    } else if (location.pathname.indexOf('ct-web') !== -1) { // 餐台厂
      this.demo = 'ct'
      this.title = '三幸运营平台(惠州市东卓新材料)'
    } else if (location.pathname.indexOf('demo-web') !== -1) { // 测试
      this.demo = 'demo'
      this.title = '三幸运营平台(测试)'
    }
    this.redLogin()
  },
  methods: {
    // 自动登录
    redLogin() {
      const user = JSON.parse(window.localStorage.getItem('loginForm'))
      if (!user?.checkLog) return
      this.loginForm.checkLog = user.checkLog
      if (this.loginForm.userName && this.loginForm.password) {
        this.handleLogin()
      } else if (user?.userName && user?.password) {
        this.loginForm = user
        this.handleLogin()
      }
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 登录
    handleLogin() {
      // this.$refs.loginForm.validate(valid => {
      if (this.loginForm.userName && this.loginForm.password) {
        this.loading = true
        this.$store
          .dispatch('login', this.loginForm)
          .then(res => {
            if (res.code !== 0) {
              this.loading = false
              this.$message({ showClose: true, message: res.message, type: 'success' })
              return
            }
            if (this.loginForm.checkLog) window.localStorage.setItem('loginForm', JSON.stringify(this.loginForm))
            window.sessionStorage.setItem('fuserId', res.data.fuserId)
            window.sessionStorage.setItem('loginToken', res.data.loginToken)
            this.$router.replace({ path: this.redirect || '/' })
            this.$message({ showClose: true, message: `${this.loginForm.userName}登录成功！`, type: 'success' })
            this.loading = false
          })
          .catch(error => {
            this.$message.error(error)
            this.loading = false
          })
      } else {
        this.$message({ showClose: true, message: '请输入用户名和密码！', type: 'warning' })
        return false
      }
      // })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 48px;
    width: 85%;

    input {
      background: transparent;
      border: 0;
      -webkit-appearance: none;
      border-radius: 0;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 48px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
  .el-form-item:last-child {
    margin-bottom: 0!important;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    //max-width: 100%;
    padding: 170px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .el-autocomplete {
    width: 100%;
  }
  .title-container {
    position: relative;

    .title {
      font-size: 32px;
      letter-spacing: 2px;
      color: $light_gray;
      margin: 0 auto 30px auto;
      font-weight: bold;
      text-align: center;
      position: relative;
    }
    /** 动画
    .title::after {
      content: '三幸运营平台';
      position: absolute;
      color: #FF3F00;
      top: 0;
      left: 0;
      width: 100%;
      height: 50%;
      //animation: loderWidth 6s linear infinite;
      overflow: hidden;
    } **/
  }
  @keyframes loderWidth {
    0% {
      height: 100%;
    }
    100% {
      height: 50%;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
